package com.wyl.mybatis.demo;

import cn.hutool.json.JSONUtil;
import com.wyl.mybatis.config.SqlSessionFactoryConfig;
import com.wyl.mybatis.entity.FullCity;
import com.wyl.mybatis.mapper.FullCityMapper;
import com.wyl.mybatis.page.Page;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;

/**
 * @Description
 * @Author WuYiLong
 * @Date 2024/3/14 11:44
 */
public class PageDemo {

    private final static Logger log = LoggerFactory.getLogger(PageDemo.class);

    public static void main(String[] args) throws IOException {
        // 获取当前的数据库名称和版本号
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryConfig.buildSqlSessionFactory();
        // SqlSessionFactory 通过解析配置文件（通常是 mybatis-config.xml）和映射文件（mapper.xml），来创建一个能够执行映射语句的会话对象SqlSession
        try(SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.SIMPLE)) {
            FullCityMapper mapper = sqlSession.getMapper(FullCityMapper.class);
            // 分页
            for (int i = 1; i <= 3 ; i++) {
                Page<FullCity> page = new Page<>(i, 10);
                List<FullCity> fullCities = mapper.selectFullCityPage(page);
                log.info("当前页：{}",page.getCurrentPage());
                log.info("页数：{}",page.getPageSize());
                log.info("总数：{}",page.getTotal());
                log.info("列表：{}", JSONUtil.toJsonStr(fullCities));
            }
        }
    }
}
